package com.sise.thread.lab01.lab03;

class ThreadVolatile extends  Thread{

    public volatile boolean flag=true;

    @Override
    public void run(){
        System.out.println("线程开始运行");
        while(flag){}
        System.out.println("线程执行完毕");
    }

    public void setFlag(boolean flag){
        this.flag=flag;
    }

}

public class Volatile {



    public static void main(String[] args) throws InterruptedException {
        ThreadVolatile tv=new ThreadVolatile();
        ThreadVolatile tv2=new ThreadVolatile();
        tv.start();
        tv2.start();
        Thread.sleep(100);
        tv.setFlag(false);  //当线程只有一个的时候主内存和线程里面的内存是可以自动同步的

    }
}
